package com.shilei.cor;

import com.shilei.Bullet;
import com.shilei.Explode;
import com.shilei.GameObject;
import com.shilei.Tank;
import com.shilei.common.Group;

/**
 * @description: 子弹坦克碰撞
 * @author: 81271
 * @date: 2020-04-15 20:17
 */
public class TankBulletCollider implements Collider {

    @Override
    public boolean collide(GameObject go1, GameObject go2) {
        if (go1 instanceof Bullet && go2 instanceof Tank) {
            Bullet bullet = (Bullet)go1;
            Tank tank = (Tank)go2;

            if (Group.Good.equals(tank.getGroup())) return false;

            if (bullet.getGroup().equals(tank.getGroup())) return false;

            if (bullet.getRect().intersects(tank.getRect())) {
                tank.die();
                bullet.die();

                int eX = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2;
                int eY = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2;
                new Explode(eX, eY);

                return true;

            }
        } else if (go1 instanceof Tank && go2 instanceof Bullet) {
            return collide(go2, go1);
        }

        return false;


    }

}
